/**
 * 
 */
package com.inkcar.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * 文件通用操作类--<code>FileUtils</code>
 * 
 * @author 欧阳增高
 */
public class FileUtils {

	/**
	 * 构造方法
	 */
	private FileUtils() {

	}

	/**
	 * 创建文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @param content
	 *            文件内容
	 * @return 创建成功返回文件大小
	 */
	public static final long create(String filePath, String content) {
		return create(filePath, content, "UTF-8");
	}

	/**
	 * 创建文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @param content
	 *            文件内容
	 * @param encoding
	 *            文件编码
	 * @return 创建成功返回文件大小
	 */
	public static final long create(String filePath, String content,
			String encoding) {
		File file = new File(filePath);
		if (!file.exists()) {
			try {
				String folderPath = file.getParent();
				if (!createFolders(folderPath)) {
					return -1;
				}
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return -1;
			}
		}
		PrintWriter myFile;
		try {
			myFile = new PrintWriter(file, encoding);
			myFile.println(content);
			myFile.close();
			return file.length();// file.getTotalSpace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * 以数据流方式获取文件内容
	 * 
	 * @param file
	 *            文件地址
	 * @return 文件数据流
	 */
	public static final InputStream readStream(String file) {
		file = decode(file);
		InputStream fs = null;
		try {
			fs = new FileInputStream(file);
		} catch (Exception e) {
			// e.printStackTrace();
		} finally {
			try {
				if (fs == null) {
					fs = FileUtils.class.getResourceAsStream(file);
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return fs;
	}

	/**
	 * 以字符串方式获取文件内容
	 * 
	 * @param file
	 *            文件地址
	 * @return 文件字符串形式内容
	 */
	public static final String readText(String file) {
		return readInputStream(readStream(file));
	}

	/**
	 * 从输入流中获取文件内容
	 * 
	 * @param input
	 *            输入流
	 * @return
	 */
	public static final String readInputStream(InputStream input) {
		try {
			if (input == null) {
				return null;
			}
			InputStreamReader isr = new InputStreamReader(input, "UTF-8");
			BufferedReader br = new BufferedReader(isr);
			StringBuilder str = new StringBuilder("");
			try {
				String data = "";
				int index = 0;
				while ((data = br.readLine()) != null) {
					if (index == 0) {
						data = data.substring(1);
					}
					str.append(data).append("\r\n");
					index++;
				}
			} catch (Exception e) {
				str.append(e.toString());
			} finally {
				br.close();
				isr.close();
			}
			return str.toString();
		} catch (IOException e) {
		}
		return null;
	}

	/**
	 * 创建多级目录
	 * 
	 * @param folderPath
	 *            目录路径
	 * @return 目录存在或创建目录成功返回true,反之false
	 */
	public static final boolean createFolders(String folderPath) {
		if (folderPath == null || "".equals(folderPath)) {
			return false;
		}
		String newFolderPath = folderPath.replaceAll("\\\\", "/").replaceAll(
				"//", "/");
		String[] arrPath = newFolderPath.split("/");
		StringBuffer sbPath = new StringBuffer("");
		for (String path : arrPath) {
			sbPath.append("/");
			if (path == null || "".equals(path) || path.indexOf(".") != -1) {
				continue;
			}
			sbPath.append(path.trim());
			boolean su = createFolder(sbPath.toString());
			if (!su) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 创建新目录
	 * 
	 * @param folderPath
	 *            目录路径
	 * @return 目录存在或创建目录成功返回true,反之false
	 */
	public static final boolean createFolder(String folderPath) {
		if (folderPath == null || "".equals(folderPath)
				|| "/".equals(folderPath) || ":".equals(folderPath)) {
			return false;
		}
		try {
			File folder = new File(folderPath);
			if (!folder.exists()) {
				folder.mkdir();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 文件路径URL转码格式解码
	 * 
	 * @param file
	 *            文件路径
	 * @return
	 * 
	 */
	public static final String decode(String file) {
		try {
			return URLDecoder.decode(file, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return file;
	}

}
